package com.zzx.redis.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import com.zzx.redis.IBaseRedisService;

public class BaseRedisServiceImpl implements IBaseRedisService{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2597845363128758685L;

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
		
	private void setExpire(String key, long timeout) {
		redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
	}

	
	public void setString(final String key, final Object cache) {
		redisTemplate.opsForValue().set(key, cache);
	}

	
	public void setString(final String key, final Object cache, long timeout) {
		redisTemplate.opsForValue().set(key, cache);
		setExpire(key, timeout);
	}

	
	public Object getString(final String key) {
		return redisTemplate.opsForValue().get(key);
	}

	
	public void appendString(final String key, final String cache) {
		redisTemplate.opsForValue().append(key, cache);
	}

	
	public void batchSetString(final Map<String, Object> cache) {
		redisTemplate.opsForValue().multiSet(cache);
	}

	
	public void incr(final String key) {
		incr(key, 1l);
	}

	
	public void incr(final String key, final Long delta) {
		redisTemplate.opsForValue().increment(key, delta);
	}

	
	public void setMap(final String key, final Map<String, Object> mapCache) {
		redisTemplate.opsForHash().putAll(key, mapCache);
	}
	
	
	public void setMap(final String key, final Map<String, Object> mapCache, long timeout) {
		redisTemplate.opsForHash().putAll(key, mapCache);
		setExpire(key, timeout);
	}

	
	public void setMapUpdateValue(final String key1, final String key2, final Object cache) {
		redisTemplate.opsForHash().put(key1, key2, cache);
	}
	
	
	public void setMapUpdateValue(final String key1, final String key2, final Object cache, long timeout) {
		redisTemplate.opsForHash().put(key1, key2, cache);
		setExpire(key1, timeout);
	}

	
	public void setMapAddValue(final String key1, final String key2, final Object cache) {
		redisTemplate.opsForHash().putIfAbsent(key1, key2, cache);
	}

	
	public Map<Object, Object> getMap(final String key) {
		return redisTemplate.opsForHash().entries(key);
	}

	
	public Object getMap(final String key1, final String key2) {
		return redisTemplate.opsForHash().get(key1, key2);
	}

	
	public List<Object> getMapList(final String key1, List<Object> key2s) {
		return redisTemplate.opsForHash().multiGet(key1, key2s);
	}

	
	public Set<Object> getMapKeys(final String key) {
		return redisTemplate.opsForHash().keys(key);
	}

	
	public List<Object> getMapValues(final String key) {
		return redisTemplate.opsForHash().values(key);
	}

	
	public Long delMapKey(final String key1, final Object... key2s) {
		return redisTemplate.opsForHash().delete(key1, key2s);
	}

	
	public Long getMapLength(final String key) {
		return redisTemplate.opsForHash().size(key);
	}

	
	public void setListL(final String key, final Object... caches) {
		redisTemplate.opsForList().leftPushIfPresent(key, caches);
	}

	
	public void setListLX(final String key, final Object... caches) {
		redisTemplate.opsForList().leftPush(key, caches);
	}

	
	public void setListR(final String key, final Object... caches) {
		redisTemplate.opsForList().rightPush(key, caches);
	}

	
	public void setListRX(final String key, final Object... caches) {
		redisTemplate.opsForList().rightPushIfPresent(key, caches);
	}

	
	public void delList(final String key, final Long count, final Object cache) {
		redisTemplate.opsForList().remove(key, count, cache);
	}

	
	public List<Object> getList(final String key, final Long start, final Long end) {
		return redisTemplate.opsForList().range(key, start, end);
	}

	
	public Object getList(final String key, final Long index) {
		return redisTemplate.opsForList().index(key, index);
	}

	
	public Long getListLength(final String key) {
		return redisTemplate.opsForList().size(key);
	}

	
	public void setSet(final String key, final Object... caches) {
		redisTemplate.opsForSet().add(key, caches);
	}
	
	
	public void setSet(final String key, long timeout, final Object... caches) {
		redisTemplate.opsForSet().add(key, caches);
		setExpire(key, timeout);
	}

	
	public Set<Object> getStet(final String key) {
		return redisTemplate.opsForSet().members(key);
	}

	
	public Set<Object> getSetDiff(final String key1, final String key2) {
		return redisTemplate.opsForSet().difference(key1, key2);
	}

	
	public Set<Object> getSetInter(final String key1, final String key2) {
		return redisTemplate.opsForSet().intersect(key1, key2);
	}

	
	public Set<Object> getUnionSet(final String key1, final String key2) {
		return redisTemplate.opsForSet().union(key1, key2);
	}

	
	public Long getSetLength(final String key) {
		return redisTemplate.opsForSet().size(key);
	}

	
	public void moveSet(final String key1, final String key2, final Object cache) {
		redisTemplate.opsForSet().move(key1, cache, key2);
	}

	
	public Object popSet(final String key) {
		return redisTemplate.opsForSet().pop(key);
	}


	public List<Object> getRandSet(final String key, final Long count) {
		return redisTemplate.opsForSet().randomMembers(key, count);
	}


	public Long delSetValue(final String key1, final Object... key2s) {
		return redisTemplate.opsForSet().remove(key1, key2s);
	}


	public Boolean existsSetValue(final String key1, final String key2) {
		return redisTemplate.opsForSet().isMember(key1, key2);
	}

	
	public Boolean exists(final String key) {
		return redisTemplate.hasKey(key);
	}

	
	public void delete(final String key) {
		redisTemplate.delete(key);
	}

	
	public int keys(final String key) {
		return redisTemplate.keys(key).size();
	}

}
